//
//  FlashlightLabel.m
//  YuiKe
//
//  Created by yuike5 yuike5 on 11-11-22.
//  Copyright (c) 2011年 yuike. All rights reserved.
//

#import "FlashlightLabel.h"

@implementation FlashlightLabel

-(void) dealloc
{
    [animationLayer release];
    [gradientLocations release];
    [gradientEndLocations release];
    [label release];
    [super dealloc];
}


-(void) setFlashText:(NSString *)text
{
    [self stopTextAnimation];
    [animationLayer release];
    [gradientLocations release];
    [gradientEndLocations release];
    [label release];
    self.backgroundColor = [UIColor blackColor];
    self.alpha = 0.8f;
    self.layer.cornerRadius=2.0f;//设置圆角的半径 
    self.layer.masksToBounds=YES;//绘制边角
    
    // label on slider track
    UIFont *labelFont = [UIFont boldSystemFontOfSize:28];
    CGSize labelSize = [text sizeWithFont:labelFont];
    label = [[UILabel alloc] initWithFrame: CGRectMake(0.0, 0.0, labelSize.width, labelSize.height)];
    [label setTextColor:[UIColor whiteColor]];
    [label setTextAlignment:UITextAlignmentCenter];
    [label setBackgroundColor:[UIColor clearColor]];
    [label setFont:labelFont];
    [label setText:text];
    [label setAlpha:1.0];
    
    // get label's layer to add animation on it
    CALayer *textLayer = [label layer];
    CGFloat textWidth = [label frame].size.width;
    CGFloat textHeight = [label frame].size.height;
    
    CGFloat x = (self.frame.size.width - textWidth) / 2.0f;
    CGFloat y = (self.frame.size.height - textHeight) / 2.0f;
    [textLayer setFrame:CGRectMake(x, y, textWidth, textHeight)];
    
    animationLayer = [[CAGradientLayer layer] retain];
    [animationLayer setFrame:CGRectMake(-textWidth * 1.0f, 0.0f, textWidth * 2.5f, textHeight)];
    [animationLayer setBackgroundColor:[[UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.45f] CGColor]];
    
    UIColor *transparentColor = [UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:1.0f];
    NSArray *colors = [NSArray arrayWithObjects:
                       (id)[[UIColor clearColor] CGColor], 
                       (id)[transparentColor CGColor], 
                       (id)[[UIColor clearColor] CGColor], nil];
    
    gradientLocations = [NSArray arrayWithObjects:
                         [NSNumber numberWithFloat:0.80], 
                         [NSNumber numberWithFloat:0.90], 
                         [NSNumber numberWithFloat:1.0], nil];
    [gradientLocations retain];
    
    gradientEndLocations = [NSArray arrayWithObjects:
                            [NSNumber numberWithFloat:0.0], 
                            [NSNumber numberWithFloat:0.10], 
                            [NSNumber numberWithFloat:0.20], nil];
    [gradientEndLocations retain];
    
    [animationLayer setColors:colors];
    [animationLayer setLocations:gradientLocations];
    
    [animationLayer setStartPoint:CGPointMake(0.0, 0.0)];
    [animationLayer setEndPoint:CGPointMake(1.0, 0.0)];
    
    [textLayer setMask:animationLayer];
    [[self layer] addSublayer:textLayer];    
    [self startTextAnimation];//[self startTextAnimation];[self startTextAnimation];[self startTextAnimation];
}


- (void) startTextAnimation 
{    
    CABasicAnimation* textAnimation = [CABasicAnimation animationWithKeyPath:@"locations"];
    [textAnimation setFromValue:gradientLocations];
    [textAnimation setToValue:gradientEndLocations];
    [textAnimation setFillMode:kCAFillModeForwards];
    [textAnimation setRepeatCount:HUGE_VALF];
    [textAnimation setDuration:4.0f];
    [textAnimation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];
    [animationLayer addAnimation:textAnimation forKey:kTextAnimationKey];
}

- (void)drawRect:(CGRect)rect
{
    [super drawRect:rect];
}

- (void) stopTextAnimation 
{
    [animationLayer removeAnimationForKey:kTextAnimationKey];
}

@end
